<template>
  <div :class="className" :style="{ height: height, width: width }"></div>
</template>

<script>
import echarts from "echarts";

export default {
  props: {
    className: {
      type: String,
      default: "lineChart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "100%",
    },
    data: {
      type: Object,
      default: null,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    datas: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      },
    },
  },
  mounted() {
    this.initChart()
  },
  methods: {
    initChart() {
      if (this.data) {
        this.chart = echarts.init(this.$el, "macarons")
        this.setOptions(this.datas)
      }
    },
    setOptions() {
      this.chart.setOption({
        title: {
          left: "center",
          text: this.data.title ? this.data.title : "",
        },
        tooltip: {
          trigger: "axis",
        },
        grid: {
          top: "15%",
          left: "5%",
          right: "5%",
          bottom: "5%",
          containLabel: true,
        },
        xAxis: {
          name: this.data.xName ? this.data.xName : "",
          nameLocation: "middle",
          nameGap: 20,
          type: "category",
          boundaryGap: false,
          splitLine: {
            show: true,
            lineStyle: {
              color: ["#000000"],
              width: 1,
              type: "solid",
            },
          },
          data: this.data.xdata ? this.data.xdata : [0],
        },
        yAxis: {
          name: this.data.yName ? this.data.yName : "",
          nameLocation: "middle",
          nameGap: 50,
          type: "value",
          splitLine: {
            show: true,
            lineStyle: {
              color: ["#000000"],
              width: 1,
              type: "solid",
            },
          },
        },
        series: [
          {
            name: "",
            type: "line",
            data: this.data.ydata ? this.data.ydata : [0],
          },
        ],
      })
    },
  },
}
</script>